import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TeacherMenu extends JFrame {
    private JTextField nameField;
    private JTextField birthField;
    private JTextField genderField;
    private JTextField collegeField;
    private JTextField positionField;
    private Teacher teacher;

    //获取教师信息的方法，返回Person类
    public Teacher queryTeacher(String tID) {
        return (Teacher)Main.controller.personalInformationQuery(tID);
    }

    //显示查询信息的菜单
    private void showQueryMenu() {
        // 显示教师信息并让用户修改
        JFrame modifyTeacherFrame = new JFrame("查询/修改教师信息");
        modifyTeacherFrame.setSize(400, 300);
        modifyTeacherFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        modifyTeacherFrame.setLocationRelativeTo(null);

        JPanel modifyTeacherPanel = new JPanel();
        modifyTeacherPanel.setLayout(new GridLayout(6, 2));

        JLabel nameLabel = new JLabel("姓名:");
        nameField = new JTextField(teacher.getName());
        modifyTeacherPanel.add(nameLabel);
        modifyTeacherPanel.add(nameField);

        JLabel birthLabel = new JLabel("出生日期:");
        birthField = new JTextField(teacher.getBirth());
        modifyTeacherPanel.add(birthLabel);
        modifyTeacherPanel.add(birthField);

        JLabel genderLabel = new JLabel("性别:");
        genderField = new JTextField(teacher.getGender());
        modifyTeacherPanel.add(genderLabel);
        modifyTeacherPanel.add(genderField);

        JLabel collegeLabel = new JLabel("学院:");
        collegeField = new JTextField(teacher.getCollege());
        modifyTeacherPanel.add(collegeLabel);
        modifyTeacherPanel.add(collegeField);

        JLabel positionLabel = new JLabel("专业:");
        positionField = new JTextField(teacher.getMajor());
        modifyTeacherPanel.add(positionLabel);
        modifyTeacherPanel.add(positionField);

        JButton confirmButton = new JButton("确认");
        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = nameField.getText().trim();
                String birth = birthField.getText().trim();
                String gender = genderField.getText().trim();
                String college = collegeField.getText().trim();
                String major = positionField.getText().trim();

                // 检查是否正确输入
                if (name.isEmpty() ||  birth.isEmpty() || gender.isEmpty() || college.isEmpty() || major.isEmpty()) {
                    JOptionPane.showMessageDialog(modifyTeacherFrame, "请输入所有信息！", "错误", JOptionPane.ERROR_MESSAGE);
                }
                else {
                    // 更新教师信息
                    teacher.setName(name);
                    teacher.setTID(Login.currentAccount);
                    teacher.setBirth(birth);
                    teacher.setGender(gender);
                    teacher.setCollege(college);
                    teacher.setMajor(major);

                    // 保存教师信息到存储中

                    JOptionPane.showMessageDialog(modifyTeacherFrame, "教师信息修改成功！", "成功", JOptionPane.INFORMATION_MESSAGE);

                    // 关闭窗口且清空文本框
                    modifyTeacherFrame.dispose();
                    nameField.setText("");
                    birthField.setText("");
                    genderField.setText("");
                    collegeField.setText("");
                    positionField.setText("");
                }
            }
        });
        modifyTeacherPanel.add(confirmButton);
        modifyTeacherFrame.add(modifyTeacherPanel);
        modifyTeacherFrame.setVisible(true);
    }

    public TeacherMenu() {
//        teacherList = new ArrayList<>();  // 初始化教师列表

        setTitle("教师菜单");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel selectionPanel = new JPanel();
        selectionPanel.setLayout(new GridLayout(2, 1));

        JButton queryButton = new JButton("查询/修改信息");
        queryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (Main.controller.logInAccountCheck(Login.currentAccount, Login.currentPwd)) {
                    String tID = Login.currentAccount;  // 根据实际情况获取当前账号对应的教师工号
                    teacher = queryTeacher(tID);  // 调用查询教师信息的方法
                    boolean found = false;
                    if (teacher != null) {
                        found = true;
                        showQueryMenu();
                    }
                    if (!found) {
                        // 如果未找到对应工号的教师，显示提示信息
                        JOptionPane.showMessageDialog(selectionPanel, "该工号的教师不存在：" + tID, "错误", JOptionPane.ERROR_MESSAGE);
                    }
                }
                else {
                    // 用户验证不通过，弹出提示窗口或者其他处理方式
                }
            }
        });
        selectionPanel.add(queryButton);
        add(selectionPanel);

        JButton exitButton = new JButton("退出");
        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();  // 关闭窗口
            }
        });
        selectionPanel.add(exitButton);
    }
}